package com.eastseeway.radioproject.config;

import static androidx.core.content.ContextCompat.startActivity;

import android.app.Application;
import android.content.Intent;
import android.provider.Settings;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.eastseeway.radioproject.R;
import com.eastseeway.radioproject.RadioApplication;
import com.eastseeway.radioproject.base.BaseViewModel;
import com.eastseeway.radioproject.base.Const;
import com.eastseeway.radioproject.entities.BaseResponse;
import com.eastseeway.radioproject.entities.BeanDeviceInfo;
import com.eastseeway.radioproject.entities.UserInfo;
import com.eastseeway.radioproject.utils.SPHelper;
import com.google.gson.Gson;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ConfigViewModel extends BaseViewModel<ConfigRepository> {
    private final MutableLiveData<String> mDeviceId = new MutableLiveData<>("");
    private final MutableLiveData<String> toastInfo = new MutableLiveData<>();
    private final MutableLiveData<Integer> progress = new MutableLiveData<>(0);
    private final MutableLiveData<Integer> pressureOffset = new MutableLiveData<>(0);
    private final MutableLiveData<UserInfo> adminUserInfo = new MutableLiveData<>();
//    仪器类型相关 台式或者立式
    private final MutableLiveData<String> machineType = new MutableLiveData<>("");
    public void onMachineTypeCheckedChange(int checkedId) {
        switch (checkedId) {
            case R.id.taishi:
                SPHelper.saveMachineType(getApplication(), Const.MACHINE_TYPE_TAISHI);
                break;
            case R.id.lishi:
                SPHelper.saveMachineType(getApplication(), Const.MACHINE_TYPE_LISHI);
                break;
        }
    }
    public MutableLiveData<String> getMachineType() {
        return machineType;
    }

    public void onSystemBarChangeListener(boolean isChecked) {
        RadioApplication.getInstance().getJwsManager().jwsSetStatusBar(isChecked);
    }

    public void onOtgChangeListener(boolean isChecked) {

    }

    public void pressureOffsetAdd(){
        pressureOffset.setValue(pressureOffset.getValue()+1);
        SPHelper.savePressureOffset(getApplication(), pressureOffset.getValue());
    }
    public void pressureOffsetSub(){
        if (pressureOffset.getValue() <= 0) {
            return;
        }
        pressureOffset.setValue(pressureOffset.getValue()-1);
        SPHelper.savePressureOffset(getApplication(), pressureOffset.getValue());
    }

    public ConfigViewModel(@NonNull Application application) {
        super(application);
        queryAdmin();
    }

    @Override
    protected ConfigRepository setRepository() {
        return new ConfigRepository(getApplication());
    }

//    设备ID相关
    public MutableLiveData<String> getDeviceId() {
        return mDeviceId;
    }
    public void checkDevice() {
        String deviceId = mDeviceId.getValue();
        if (deviceId.isEmpty()) {
            toastInfo.setValue("id不能为空!");
            return;
        }
        mRepository.checkDevice(deviceId, new Callback<BaseResponse<BeanDeviceInfo>>() {
            @Override
            public void onResponse(Call<BaseResponse<BeanDeviceInfo>> call, Response<BaseResponse<BeanDeviceInfo>> response) {
                if (response.code() == 200) {
                    BeanDeviceInfo result = response.body().getResult();
                    if (result.getDeviceId().equals(deviceId)){
                        SPHelper.setDeviceId(getApplication(), deviceId);
                        toastInfo.setValue("设置成功!");
                    }
                } else {
                    toastInfo.setValue("设备码不存在!");
                }
            }

            @Override
            public void onFailure(Call<BaseResponse<BeanDeviceInfo>> call, Throwable throwable) {
            }
        });
    }

    public void queryAdmin() {
        mRepository.queryAdmin().observeForever(adminUserInfo::setValue);
    }

    public void updateOperationParamsIsEmpty(){
        mRepository.updateOperationParamsIsEmpty(adminUserInfo.getValue());
    }


    public MutableLiveData<Integer> getProgress() {
        return progress;
    }

    public MutableLiveData<Integer> getPressureOffset() {
        return pressureOffset;
    }
}
